@tool
extends IEventBehavior


class_name DoEffect


## 需要监听的事件类型
@export var event_type: CharacterUnit.Event = CharacterUnit.Event.NONE
## 需要触发的行为类型
@export var effect_type: CharacterUnit.Effect = CharacterUnit.Effect.NONE

## 需要提取的角色键值(""表示使用自身)
@export var character_key: String = ""
## 是否直接引用原始事件参数
@export var is_direct_event: bool = false:
    set(v):
        is_direct_event = v
        notify_property_list_changed()

## 需要从原始事件中提取的键值表达式
@export var meta_expressions: Dictionary[String, String] = {}
## 需要附加的额外参数
@export var additional_meta: Dictionary[String, Variant] = {}


func _validate_property(property: Dictionary) -> void:
    if ["meta_expressions", "additional_meta", "effect_makers"].has(property.name):
        if not is_direct_event:
            property.usage |= PROPERTY_USAGE_NO_EDITOR
        else:
            property.usage |= ~(PROPERTY_USAGE_NO_EDITOR)


func support() -> Array:
    return [0, event_type]

func do_event(character: CharacterComponent, v: EffectValue, e: CharacterEvent) -> void:
    var c: CharacterComponent = character if character_key == "" else e.meta.get(character_key)
    if c == null:
        push_error("CharacterEvent.meta get character error:", character_key, "=>", e.meta)
        return
    if is_direct_event:
        c.on_effect(effect_type, e)
        return
    var new_e: CharacterEvent = CharacterEvent.new()
    new_e.type = e.type
    for k in meta_expressions:
        var expression: String = meta_expressions[k]
        if expression == "":
            new_e.meta[k] = e.meta[k]
        else:
            new_e.meta[k] = NodeHelper.evaluate(self, expression, ["c", "value", "meta"], [c, v.value, e.meta])
    for k in additional_meta:
        new_e.meta[k] = additional_meta[k]
    c.on_effect(effect_type, new_e)
